package com.threeox.commonlibrary.adapter.base;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.util.CommonUtils;
import com.threeox.commonlibrary.util.ViewUtils;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;
import com.threeox.utillibrary.util.res.IdHelper;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *
 * @ClassName: AutoIntoValAdapter 
 *
 * @Description: TODO(自动注入的Adapter) 
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016-9-13 上午11:33:12 
 *
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class AutoIntoValAdapter<T> extends CommonAdapter<T> {

	// private Map<Integer, Object> mAutoVal = new HashMap<Integer, Object>();
	// private Class<? extends Object> _Cls = null;
	protected IdHelper mIdHelper = null;
    protected ViewUtils mViewUtils = null;

	public AutoIntoValAdapter() {
	}

	public AutoIntoValAdapter(Context context, List<T> datas) {
		super(context, datas);
	}

	public AutoIntoValAdapter(Context context, List datas, int layoutId) {
		super(context, datas, layoutId);
	}

	@Override
	public void conver(BaseViewHolder holder, int position, int viewType, T data) {
		try {
			Long startTime = System.currentTimeMillis();
			// LogUtils.d(TAG, "开始自动注入:" + startTime);
			injectObj(holder, data);
			Long stopTime = System.currentTimeMillis();
			LogUtils.d(TAG, "结束自动注入:" + stopTime + "用时:" + (stopTime - startTime));
		} catch (Exception e) {
            LogUtils.e(TAG, "conver函数调用报错了:" + e.getMessage());
		}
	}

    /**
     * 注入对象
     *
     * @param holder
     * @param obj
     */
	public void injectObj(BaseViewHolder holder, Object obj) {
		List<Object> tags = holder.getViewTags();
		if (EmptyUtils.isNotEmpty(tags)) {
			for (Object tag : tags) {
				try {
					if (tag != null) {
						View view = holder.getView(tag);
                        injectView(obj, tag.toString(), view);
					}
				} catch (Exception e) {
                    LogUtils.e(TAG, "injectObj报错了:" + e.getMessage());
				}
			}
		}
	}
	
	/**
	 * 根据Id自动注入
	 * 
	 * @param holder
	 * @param obj
	 */
	public void injectObj2Id(BaseViewHolder holder, Object obj){
		try {
			if (obj instanceof JSONObject) {
				parseJson(holder, (JSONObject) obj);
			} else {
				parseObj(holder, obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 解析对象
	 * 
	 * @param holder
	 * @param obj
	 */
	protected void parseObj(BaseViewHolder holder, Object obj) {
		Class<? extends Object> clazz = obj.getClass();
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			try {
				String key = field.getName();
				if (mIdHelper == null) {
					mIdHelper = IdHelper.newInstance(mContext);
				}
				int id = mIdHelper.getViewId(key);
				Method method = clazz.getMethod("get" + CommonUtils.captureName(key));
				holder.setValue(id, method.invoke(obj));
			} catch (Exception e) {
				// LogUtils.e(TAG, "注入失败:" + e.getMessage());
			}
		}
	}

	/**
	 * 解析JSONObject
	 * 
	 * @param holder
	 * @param obj
	 */
	protected void parseJson(BaseViewHolder holder, JSONObject obj) {
		Set<String> set = obj.keySet();
		for (String key : set) {
			try {
				if (mIdHelper == null) {
					mIdHelper = IdHelper.newInstance(mContext);
				}
				int id = mIdHelper.getViewId(key);
				holder.setValue(id, obj.get(key));
			} catch (Exception e) {
				LogUtils.e(TAG, "注入失败:" + e.getMessage());
			}
		}
	}

    /**
     * View 注入数据
     *
     * @param obj
     * @param tag
     * @param view
     */
	protected void injectView(Object obj, String tag, View view) {
        if (mViewUtils == null) {
			mViewUtils = ViewUtils.newInstance();
		}
        mViewUtils.injectView(obj, tag.toString(), view);
	}

}
